﻿var sid = 0;
var ids;

$(function() {
	$('#inactholder input:radio').bind('click', function() {inactModeChanges($(this).val()); });
	$('#btnact').bind('click', makeAct);
});

function getList() {
    reloadTable(tableUrl, { nolayout: 'true', timestamp: new Date() }, initTable);   
}

function statusIdChange(obj, statusid) {
    var obj = $(obj);
    if (obj.parent().hasClass('selected')) return false;
	$(tableRootDiv).load(tableUrl, { tb_nf_statusid: statusid, nolayout: 'true', timestamp: new Date() }, function() {
		initTable();
		$('ul.statusfilterlist li.selected').removeClass('selected');
		obj.parent().addClass('selected');
		sid = statusid;
		reloadStatusList();
	});		
    return false;
}

function resetRange() {
	$(tableRootDiv).load(tableUrl, { tb_rangeId: 0, nolayout: 'true', timestamp: new Date() }, function() {
		initTable();
		$('#rangeholder').hide();
		reloadStatusList();
	});
    return false;
}

function checkDeleteButton(statusid) {
	var holder = $('#invalidoperations');
	var issubselected = $('ul.statusfilterlist.rightfilterlist li:.selected:first').hasClass('sub');
	if (statusid == -1 || issubselected)
		holder.show();
	else
		holder.hide();
	return false;
}

function reloadStatusList() {
    $('#statuslist').load(base + 'notarialform.ivp/getstatusesinfo', { nolayout: 'true', timestamp: new Date() }, function() {checkDeleteButton(sid);}); 
}

function inactModeChanges(mode) {
	var btnact = $('a#btnact');
	(mode == 0) ? btnact.show() : btnact.hide();
	 
	$.post("notarialform.ivp/ChangeActMode/",
			{ timestamp: new Date(), mode: mode}, 
			function() {reloadTable(tableUrl, { nolayout: 'true', timestamp: new Date() }, initTable);});
		
	return false;
}

function makeAct() {
	if (selectedIds.length == 0) { alert('Отметьте галочками записи, которые вы хотите включить в акт передачи.'); return false; }
	var ids = selectedIds.join(',');
	var win = window.open(base + tableController + 'excel?ids='+ids);
	setTimeout(setInActAttribute, 3000);
	return false;
}

function setInActAttribute() {
	var ids = selectedIds.join(',');
	$.post("notarialform.ivp/SetInActAttribute/", {ids:ids, timestamp: new Date()}, 
		function() {			
			selectedIds.length = 0;
			reloadTable(tableUrl, { nolayout: 'true', timestamp: new Date() }, initTable);
		}
	);
}

////action menu
function showActionMenu(obj, notarialformid) {
    removeActionMenu();
    
    var obj = $(obj);
    var offset = obj.offset();

    $('body').append('<div id="actionmenuholder" class="actionmenuholder hidden widtрpx" onBlur="return removeActionMenu();"></div>');

    var menu = $('#actionmenuholder');

    menu.load('notarialform.ivp/getActionMenu', { nolayout: 'true', timestamp: new Date(), notarialformid: notarialformid }, function() {
        menu.addClass('popupwrapper').css('top', offset.top + obj.height() - 5).css('left', offset.left);
        menu.bgiframe().show();
    });
                                   
    return false;
}

function removeActionMenu() {
    var menuholder = $('#actionmenuholder');
    
    if (menuholder.length > 0)
        menuholder.remove();

    return false;
}

function changeStatus(id, statusid) {
    $.getJSON(base+'notarialform.ivp/changestatus', {json: true, timestamp: new Date(), id: id, statusid: statusid}, 
		function(result) {
			if (result.hasOwnProperty('IsOK') && result.IsOK == true) {
				reloadStatusList();				
				getList();
				removeActionMenu();
				alert('Операция выполнена успешно');
			}
			else {
				alert('Операция не выполнена.\r\n' + result.Description);
			}
		});

    return false;
}